package com.yue.ssa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yue.ssa.bean.UserRole;
import com.yue.ssa.mapper.UserRoleMapper;
import com.yue.ssa.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhengyl
 * @description
 * @date 2020-07-31 20:23
 */
@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    @Override
    public boolean assignRoles(String uid, String[] rids) {
        //删除原先的映射关系
        Map<String,Object> params=new HashMap<>();
        params.put("uid",uid);
        this.removeByMap(params);
        List<UserRole> userRoles = new ArrayList<>();
        for (String rid : rids) {
            UserRole userRole = new UserRole();
            userRole.setRid(rid);
            userRole.setUid(uid);
            userRoles.add(userRole);
        }
        //批量保存
        return this.saveBatch(userRoles);
    }
}
